package com.car.file;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/** 文本文件读写 以及 换行乱码处理 */
public class TextFile
{
    private static String basePath = "C:\\alibaba";
    private static String basePath2 = "C:\\Documents and Settings\\Administrator\\桌面\\park\\sam";


    public static void main2(String[] args) throws IOException
    {
        String contentAll = read();

        String baseName = "";

        for (int i = 1; i < 10; i++)
        {
            String content = new String(contentAll);
            baseName = "sam00";
            content = content.replace("sam001", baseName + i);
           // write(content, baseName + i);
        }

        for (int i = 10; i < 61; i++)
        {
            String content = new String(contentAll);
            baseName = "sam0";
            content = content.replace("sam001", baseName + i);
          //  write(content, baseName + i);
        }
    }


    public static String read() throws IOException
    {
        String path = basePath + "\\userError.txt";
        File file = new File(path);
        FileInputStream in = new FileInputStream(file);
        InputStreamReader isr = new InputStreamReader(in, "utf-8");
        BufferedReader br = new BufferedReader(isr);

        String content = "";

        String s1 = null;
        while ((s1 = br.readLine()) != null)
        {
            content += s1 + "\n";
        }
        br.close();
        isr.close();

        System.out.println(content);

        return content;
    }


    public static void writeTest(String content, String fileName)
            throws IOException
    {
        String path2 = basePath2 + "\\" + fileName + ".ini";
        File file2 = new File(path2);
        FileOutputStream out = new FileOutputStream(file2);
        OutputStreamWriter osw = new OutputStreamWriter(out, "utf-8");
        BufferedWriter bwr = new BufferedWriter(osw);

        bwr.write(content);
        bwr.flush();
        bwr.close();
    }


    public static void main(String[] args) throws IOException
    {
        String file0 = "c:\\a.txt";
       

        String newline0 = "\n";
        String newline1 = "\r\n";

        String text = "this is test text!";

        String content = text + newline0 + text + newline0 + text + newline0;
        String read=read();
        content=read+content;
        write(content);

       
    }


    public static void write(String content)
            throws IOException
    {
    	 String newline = ";";
         String nextline = "\r\n";
         String path = basePath + "\\userError.txt";
        FileOutputStream out = new FileOutputStream(path);
        OutputStreamWriter osw = new OutputStreamWriter(out, "utf-8");
        BufferedWriter bwr = new BufferedWriter(osw);

        bwr.write( newline + content + nextline);
        bwr.flush();
        bwr.close();
    }
}